package com.ruoyi.web.controller.admin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.PetAdoption;
import com.ruoyi.system.domain.PetInfo;
import com.ruoyi.system.domain.vo.PetAdoptionVo;
import com.ruoyi.system.mapper.PetCategoryMapper;
import com.ruoyi.system.mapper.PetInfoMapper;
import com.ruoyi.system.service.IPetAdoptionService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 领养申请Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/admin/adopt")
public class AdminAdoptController extends BaseController
{
    @Autowired
    private IPetAdoptionService petAdoptionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private PetInfoMapper petInfoMapper;

    @Autowired
    private PetCategoryMapper petCategoryMapper;
    /**
     * 查询领养申请列表
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:list')")
    @GetMapping("/list")
    public TableDataInfo list(PetAdoption petAdoption)
    {
        startPage();
        petAdoption.setStatus("1"); //已领养
        List<PetAdoption> list = petAdoptionService.selectPetAdoptionList(petAdoption);
        List<PetAdoptionVo> collect = list.stream().map(item -> {
            PetAdoptionVo petAdoptionVo = new PetAdoptionVo();
            BeanUtils.copyProperties(item, petAdoptionVo);
            PetInfo petInfo = petInfoMapper.selectPetInfoById(item.getPetId());
            if (ObjectUtils.isNotEmpty(petInfo)) {
                petAdoptionVo.setPetId(petInfo.getPetId());
                petAdoptionVo.setPetName(petInfo.getName());
                petAdoptionVo.setAge(petInfo.getAge());
                petAdoptionVo.setGender(petInfo.getGender());
                petAdoptionVo.setCategoryName(petInfo.getCategoryName());
                petAdoptionVo.setImage(petInfo.getImage());
            }
            return petAdoptionVo;
        }).collect(Collectors.toList());
        return getDataTable(collect);
    }


    /**
     * 获取领养申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:query')")
    @GetMapping(value = "/get/{adoptionId}")
    public AjaxResult getInfo(@PathVariable("adoptionId") Long adoptionId)
    {
        PetAdoption petAdoption = petAdoptionService.selectPetAdoptionById(adoptionId);
        PetAdoptionVo petAdoptionVo = new PetAdoptionVo();
        BeanUtils.copyProperties(petAdoption, petAdoptionVo);
        PetInfo petInfo = petInfoMapper.selectPetInfoById(petAdoption.getPetId());
        if (ObjectUtils.isNotEmpty(petInfo)) {
            petAdoptionVo.setPetId(petInfo.getPetId());
            petAdoptionVo.setPetName(petInfo.getName());
            petAdoptionVo.setAge(petInfo.getAge());
            petAdoptionVo.setGender(petInfo.getGender());
            petAdoptionVo.setCategoryName(petInfo.getCategoryName());
            petAdoptionVo.setImage(petInfo.getImage());
        }
        return success(petAdoptionVo);
    }

    /**
     * 新增领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:add')")
    @Log(title = "领养申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PetAdoption petAdoption)
    {
        PetAdoption  petAdoptionOld= petAdoptionService.getByPetId(petAdoption.getPetId());
        if(ObjectUtils.isNotEmpty(petAdoptionOld)){
            throw new BaseException("当前动物已经被领养");
        }
        return toAjax(petAdoptionService.insertPetAdoption(petAdoption));
    }

    /**
     * 修改领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:edit')")
    @Log(title = "领养申请", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PetAdoption petAdoption)
    {
        return toAjax(petAdoptionService.updatePetAdoption(petAdoption));
    }

    /**
     * 删除领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:remove')")
    @Log(title = "领养申请", businessType = BusinessType.DELETE)
    @PostMapping("/del/{adoptionIds}")
    public AjaxResult remove(@PathVariable Long[] adoptionIds)
    {
        return toAjax(petAdoptionService.deletePetAdoptionByIds(adoptionIds));
    }

    /**
     * 审批领养申请
     */
    @PreAuthorize("@ss.hasPermi('pet:adopt:approve')")
    @Log(title = "领养申请", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody PetAdoption petAdoption)
    {
        return toAjax(petAdoptionService.approveAdoption(petAdoption));
    }
}
